package com.xiaozeng.product.controller;

import com.xiaozeng.param.ProductHotParam;
import com.xiaozeng.param.ProductIntegerParam;
import com.xiaozeng.param.ProductPromoParam;
import com.xiaozeng.param.ProductCollectParam;
import com.xiaozeng.pojo.Product;
import com.xiaozeng.product.service.ProductService;
import com.xiaozeng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author: xiaozeng
 * @Date: 2022 11
 * @Description:
 **/
@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;


    /**
     * 根据传入的类别名称，进行商品名称查询，查询销售量最多的商品信息
     * @param productPromoParam 接收检查的账号实体 内部有参数校验注解
     */
    @PostMapping("/promo")
    public Result promo(@RequestBody @Validated ProductPromoParam productPromoParam, BindingResult result){

        if (result.hasErrors()){
            return Result.fail("数据查询失败!");
        }
        return  productService.promo(productPromoParam.getCategoryName());
    }

    /**
     * 根据传入的类别名称，进行商品名称查询，查询销售量最多的商品信息
     * @param productHotParam 类别List
     */
    @PostMapping("/hots")
    public Result hots(@RequestBody @Validated ProductHotParam productHotParam, BindingResult result){

        if (result.hasErrors()){
            return Result.fail("数据查询失败!");
        }
        return  productService.hots(productHotParam);
    }

    @PostMapping("category/list")
    public Result cList(){
        return productService.cList();
    }

    /**
     * 类别查询
     * @param productIntegerParam：为空时返回全部
     */
    @PostMapping("bycategory")
    public Object byCategory(@RequestBody ProductIntegerParam productIntegerParam){
        return productService.byCategory(productIntegerParam);
    }

    /**
     * 查询全部商品,可以复用业务
     */
    @PostMapping("all")
    public Object all(@RequestBody ProductIntegerParam productIntegerParam){
        return productService.all(productIntegerParam);
    }
    /**
     * 商品详情接口
     */
    @PostMapping("detail")
    public Object detail(@RequestBody Map<String,Integer> param){
        //从传入的JSON参数中获取productID
        Integer productID = param.get("productID");
        return productService.detail(productID);
    }
    /**
     * 商品图片接口
     */
    @PostMapping("pictures")
    public Object productPictures(@RequestBody Map<String,Integer> param){
        Integer productID = param.get("productID");
        return productService.pictures(productID);
    }

    /**
     * 供收藏服务使用,根据传入的id,查询商品集合!
     */
    @PostMapping("ids")
    public List<Product> list(@RequestBody ProductCollectParam productCollectParam){

        return productService.ids(productCollectParam);
    }

}
